var t = require("../../utils/util.js");

Component({
    properties: {
        mask: {
            type: Boolean,
            value: !0
        },
        show: {
            type: Boolean,
            value: !1
        },
        countDownNum: {
            type: Number,
            value: 60
        },
        isLiveConfirm: {
            type: Boolean,
            value: !1
        }
    },
    data: {
        recordInfo: null,
        recorderManager: null,
        innerAudioContext: null,
        timer: "",
        status: 0,
        countTime: 0
    },
    ready: function() {
        this.data.recorderManager = wx.getRecorderManager(), this.initRecorderManager(), 
        this.data.innerAudioContext = wx.createInnerAudioContext(), this.initInnerAudio(), 
        this.setData({
            countTime: this.data.countDownNum,
            status: 0
        });
    },
    detached: function() {
        t.log("record => detached回收不需要的对象"), this.data.recordInfo = null, null != this.data.recorderManager && 1 == this.data.status && this.data.recorderManager.stop(), 
        null != this.data.innerAudioContext && (this.data.innerAudioContext.stop(), this.data.innerAudioContext.destroy(), 
        this.data.innerAudioContext = null), null != this.data.timer && clearInterval(this.data.timer), 
        this.data.status = 0, this.data.countTime = 0;
    },
    methods: {
        stopTouch: function(t) {},
        onRecordButton: function() {
            1 != this.data.status ? 2 != this.data.status && 3 != this.data.status ? this.readyToRecord() : this.startListen() : this.data.recorderManager.stop();
        },
        readyToRecord: function() {
            var a = this;
            t.checkPermission("scope.record", "请到设置页面授权“麦克风”权限", function() {
                a.startRecord();
            });
        },
        initRecorderManager: function() {
            var a = this;
            a.data.recorderManager.onStart(function() {
                t.log("record => record start"), a.countDown(), a.setData({
                    status: 1
                });
            }), a.data.recorderManager.onPause(function(e) {
                t.log("record => record pause, 以下是其信息"), t.log(e), a.data.recorderManager.stop();
            }), a.data.recorderManager.onStop(function(e) {
                clearInterval(a.data.timer), t.log("record => record stop, 以下是其信息"), t.log(e);
                var n = e.duration;
                if (n < 1e3) return t.showToast("录音时长过短，请重新录制"), void a.setData({
                    recordInfo: null,
                    timer: "",
                    countTime: a.data.countDownNum,
                    status: 0
                });
                var o = Math.ceil(n / 1e3);
                a.setData({
                    recordInfo: e,
                    timer: "",
                    countTime: o,
                    status: 1 == a.data.status ? 2 : 0
                });
            }), a.data.recorderManager.onError(function(a) {
                t.log("record => record error");
            });
        },
        startRecord: function() {
            var t = {
                duration: 1e3 * this.data.countTime,
                sampleRate: 44100,
                numberOfChannels: 1,
                encodeBitRate: 192e3,
                format: "aac",
                frameSize: 50
            };
            this.data.recorderManager.start(t);
        },
        initInnerAudio: function() {
            var a = this;
            a.data.innerAudioContext.autoplay = !1, a.data.innerAudioContext.onPlay(function() {
                t.log("开始试听了 = >>>>>>>>" + a.data.status), a.playCoundDown(), a.setData({
                    status: 3
                });
            }), a.data.innerAudioContext.onError(function(e) {
                if (t.log("试听出错 = >>>>>>>>" + a.data.status), clearInterval(a.data.timer), null != a.data.recordInfo) {
                    var n = a.data.recordInfo.duration, o = Math.ceil(n / 1e3);
                    a.setData({
                        status: 2,
                        countTime: o,
                        timer: ""
                    });
                }
            }), a.data.innerAudioContext.onPause(function(e) {
                t.log("试听暂停 = >>>>>>>>" + a.data.status), t.log(e), a.data.innerAudioContext.stop();
            }), a.data.innerAudioContext.onEnded(function() {
                if (t.log("试听结束 = >>>>>>>>" + a.data.status), clearInterval(a.data.timer), null != a.data.recordInfo) {
                    var e = a.data.recordInfo.duration, n = Math.ceil(e / 1e3);
                    a.setData({
                        status: 2,
                        countTime: n,
                        timer: ""
                    });
                }
            }), a.data.innerAudioContext.onStop(function() {
                if (t.log("试听停止 = >>>>>>>>" + a.data.status), clearInterval(a.data.timer), null != a.data.recordInfo) {
                    var e = a.data.recordInfo.duration, n = Math.ceil(e / 1e3);
                    a.setData({
                        status: 2,
                        countTime: n,
                        timer: ""
                    });
                }
            });
        },
        startListen: function() {
            var a = this;
            2 == a.data.status ? (t.log("准备开始试听了 = >>>>>>>>" + a.data.status), a.data.innerAudioContext.src = a.data.recordInfo.tempFilePath, 
            a.data.innerAudioContext.play()) : 3 == a.data.status && a.data.innerAudioContext.stop();
        },
        countDown: function() {
            var t = this, a = t.data.countTime;
            t.setData({
                timer: setInterval(function() {
                    --a > 0 || (a = 0), t.setData({
                        countTime: a
                    }), a <= 0 && t.data.recorderManager.stop();
                }, 1e3)
            });
        },
        playCoundDown: function() {
            var t = this, a = t.data.countTime;
            t.setData({
                timer: setInterval(function() {
                    --a > 0 || (a = 0), t.setData({
                        countTime: a
                    }), a <= 0 && null != t.data.innerAudioContext && t.data.innerAudioContext.stop();
                }, 1e3)
            });
        },
        onDeleteButton: function() {
            this.setData({
                recordInfo: null,
                timer: "",
                countTime: this.data.countDownNum,
                status: 0
            });
        },
        confirmTap: function(t) {
            0 != this.data.status ? 1 != this.data.status ? 3 != this.data.status ? (this.triggerEvent("confirmtap", {
                audioInfo: this.data.recordInfo
            }, {}), this.setData({
                status: 0,
                countTime: this.data.countDownNum
            })) : wx.showToast({
                title: "正在试听录音",
                icon: "none"
            }) : wx.showToast({
                title: "正在录音，请点击结束",
                icon: "none"
            }) : wx.showToast({
                title: "还没有录音",
                icon: "none"
            });
        },
        cancelTap: function() {
            if (1 != this.data.status) if (3 != this.data.status) {
                this.data;
                this.setData({
                    show: !1,
                    recordInfo: null,
                    status: 0,
                    countTime: this.data.countDownNum
                }), this.triggerEvent("close", {}, {});
            } else wx.showToast({
                title: "正在试听录音，结束后才能取消",
                icon: "none"
            }); else wx.showToast({
                title: "正在录音，结束后才能取消",
                icon: "none"
            });
        },
        close: function() {
            if (1 != this.data.status && 3 != this.data.status) {
                this.data;
                this.setData({
                    show: !1,
                    recordInfo: null,
                    status: 0,
                    countTime: this.data.countDownNum
                }), this.triggerEvent("close", {}, {});
            }
        },
        stopEvent: function() {}
    }
});